<?php

namespace App\Http\Controllers;

use App\MethodLog;
use App\Order;
use App\Product;
use App\ProductClass;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Admin;

class AdminController extends Controller
{
    /*
     * login
     * 渲染登录页面
     * */
    public function login(){
        return view('admin.login');
    }


    /**
     * sign
     * 渲染注册页面
     * 优化 2019-7-23 11:22:55
     */
    public function sign(){
        return view('admin.sign');
    }


    /*
     * signDo
     * 注册数据校验
     * 优化 2019-7-23 11:23:00
     * */
    public function signDo(Request $request){
        $data = Admin::create([
            'username'=>$request->username,
            'password'=>$request->password,
        ]);
        if($data){
//            $result['code']='200';
//            $result['message']='注册成功';
            return view('admin.login')->with(['message'=>'注册成功','url'=>url("views/admin/login")]);
        }else{
            $result['code']='400';
            $result['message']='注册失败';
            $result['data']=null;
        }
    }


    /*
     * loginDo
     * 登陆数据校验
     * 优化 2019-7-23 11:22:43
     * */
    public function loginDo(Request $request){
        $token =  md5($request->username.$request->password.time());
        $data = Admin::where(['username'=>$request->username,'password'=>$request->password])->first();
        $rootname =$data['username'];
        $id = $data['id'];
        $data->update([
            'token'=>$token,
        ]);
        $data=json_decode($data,true);
        if($data){
            $request->session()->put('id',$id);
            $request->session()->put('name',$rootname);
            $username = $data['username'];
            $request->session()->put('username',$username);
            $name=$request->session()->get('username');
            MethodLog::create([
                'username'=>$name,
                'content'=>$name.'登录后台',
            ]);

            $result['code']='200';
            $result['message']='登陆成功';
            $result['token']=$token;
            return $result;
        }else{
            $result['code']='400';
            $result['message']='登陆失败';
            $result['data']=null;
            return $result;
        }
    }

    /*
     * edit
     * 退出登录
     * */
    public function edit(Request $request){
        $request->session()->pull('username');
        return redirect('login');
    }


    /*
     * first
     * 渲染主页面
     * 优化 2019-7-23 11:22:25
     * */
    public function first(){
        return view('admin.first');
    }


    /*
     * welcome
     * 渲染welcome页面
     * 优化 2019-7-23 11:22:20
     * */
    public function welcome(Request $request){

        //订单数量
        $orderCount = Order::count();
        //交易总额度
        $priceSum = 0;
        $sum = Order::get();
        foreach ($sum as $k=>$v){
            $priceSum = $priceSum+$v->price;
        }
        //产品数量
        $productCount = Product::count();
        //会员数量
        $superMan = Admin::count();

        $name = $request->session()->get('name');
        $data = MethodLog::where(['username'=>$name])->first();
//        $created_at = $data->created_at;
        return view('admin.welcome',compact('name','orderCount','priceSum','productCount','superMan'));
    }

    /*
     * savePwd
     * 修改密码（渲染模板）
     * 优化2019-7-25 20:40:20
     * */
    public function savePwd(Request $request){
        $id = $request->session()->get('id');
        $data = Admin::where(['id'=>$id])->first();
        return view('admin.savepwd',compact('data'));
    }

    /*
     *savePwdDo
     * 修改密码
     * 优化2019-7-25 20:51:20
     * */
    public function savePwdDo(Request $request){
        $id = $request->session()->get('id');
        $data = Admin::findOrFail($id);
        $res = $data->update([
            'password'=>$request->password,
        ]);
        if($res){
            $name=$request->session()->get('username');
            MethodLog::create([
                'username'=>$name,
                'content'=>$name.'修改密码为:'.$request->password,
            ]);
        }
        return view('admin.login');
    }

    /*
     * productClass
     * 商品分类展示
     * 优化2019-7-23 11:21:54
     * */
    public function productClass(){
        $data = ProductClass::paginate(3);
        return view('admin.productClass',compact('data'));
    }

    /*
     * proClassAdd
     * 添加新分类
     * 优化 2019-7-23 11:26:19
     * */
    public function proClassAdd(Request $request){
        $data = ProductClass::create([
            'classname'=>$request->classname,
            'parent_id'=>$request->parent_id,
        ]);

        //成功返回原页面
        if($data){
            $name=$request->session()->get('username');
            MethodLog::create([
                'username'=>$name,
                'content'=>$name.'添加新分类:'.$request->classname,
            ]);
            return back();
        }
    }


    /*
     * proClassEdit
     * 编辑分类
     * 优化 2019-7-23 12:11:19
     * */
    public function proClassEdit($id){
        $data = ProductClass::findOrFail($id);
        return view('admin.proClassEdit',compact('data'));
    }


    /*
     * proClassEditDo
     * 修改分类
     * 优化 2019-7-23 12:17:39
     * */
    public function proClassEditDo(Request $request,$id){
        $data = ProductClass::findOrFail($id);
        $data->update([
            'name'=>$request->classname,
            'parent_id'=>$request->parent_id,
        ]);
        $name=$request->session()->get('username');
        MethodLog::create([
            'username'=>$name,
            'content'=>$name.'修改id为'.$id.'的分类',
        ]);
     return back();
    }

    /*
     * proClassEditDel
     * 删除分类
     * 优化 2019-7-23 12:35:46
     * */
    public function proClassEditDel(Request $request , $id){
        $data = ProductClass::findOrFail($id);
        $data -> delete();
        $name=$request->session()->get('username');
        MethodLog::create([
            'username'=>$name,
            'content'=>$name.'删除id为'.$id.'的分类',
        ]);
        return back();
    }


        /*
         * project_Administrator
         * 从数据库中读取数据展示到商品页面
         * */
    public function project_Administrator(){
        $data = Product::get();
        return view('admin.project',compact('data'));
    }
//
        /*
         * save
         * 修改商品信息
         * */
    public function save($id){
        $data = Product::findOrFail($id);
        return view('admin.update',compact('data'));
    }


    public function do_upt(Request $request,$id){
        $data = Product::findOrFail($id);
        $data->update([
            'title'=>$request->title,
            'img'=>$request->img,
            'catagory_id'=>$request->catagory_id,
            'price'=>$request->price,
            'inventory'=>$request->inventory,
        ]);
        $name=$request->session()->get('username');
        MethodLog::create([
            'username'=>$name,
            'content'=>$name.'修改id为'.$id.'产品信息',
        ]);
        return back();
    }

        /*
         * insert()
         * 产品添加页面
         * */
    public function insert(){
        return view('admin.insert');
    }
    public function do_ist(Request $request){
        //添加到数据库
//        var_dump($request->title);
       $res = Product::create([
            'title'=>$request->title,
            'img'=>$request->img,
            'catagory_id'=>$request->catagory_id,
            'price'=>$request->price,
            'inventory'=>$request->inventory,
        ]);
       if($res){
           $name=$request->session()->get('username');
           MethodLog::create([
               'username'=>$name,
               'content'=>$name.'产品添加:'.$request->title,
           ]);
           return back();
       }else{
           echo '添加失败';
       }
    }
//
    public function proDel(Request $request,$id){
        $data = Product::FindOrFail($id);
        $res = $data->delete();
        if($res){
            $name=$request->session()->get('username');
            MethodLog::create([
                'username'=>$name,
                'content'=>$name.'删除产品ID:'.$id,
            ]);
            return back();
        }else {
            echo '删除失败';
        }
    }
}
